package com.qualityevaluationclouduser.service.impl;

import com.qualityevaluationclouduser.entity.MenuInfo;
import com.qualityevaluationclouduser.entity.RoleFunction;
import com.qualityevaluationclouduser.repository.MenuInfoRepository;
import com.qualityevaluationclouduser.repository.RoleFunctionRepository;
import com.qualityevaluationclouduser.service.MenuInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.criteria.Predicate;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @program: qualityEvaluationCloudProject
 * @description: 权限信息业务实现类
 * @author:
 * @create:
 **/
@Transactional
@Service
public class MenuInfoServiceImpl implements MenuInfoService {

    @Autowired
    private MenuInfoRepository menuInfoRepository;
    @Autowired
    RoleFunctionRepository roleFunctionRepository;

    @Override
    public MenuInfo addMenuInfo(MenuInfo menuInfo) {
        MenuInfo result = new MenuInfo();
        try {
            if (menuInfoRepository.existsMenuInfoByMenuname(menuInfo.getMenuname()))
                throw new RuntimeException("权限已存在");
            menuInfo.setCreationdate(new Date());
            result = menuInfoRepository.save(menuInfo);
            RoleFunction roleFunction = new RoleFunction();
            roleFunction.setMenuid(result.getId());
            roleFunction.setRoleid(Long.parseLong("3"));
            roleFunction.setCreationdate(new Date());
            roleFunctionRepository.save(roleFunction);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    @Transactional
    @Override
    public void delMenuInfo(String id) {
        try {
            MenuInfo MenuInfo = menuInfoRepository.findById(Long.parseLong(id)).get();
            if (null != MenuInfo) {
                menuInfoRepository.deleteById(MenuInfo.getId());
                roleFunctionRepository.deleteAllByMenuid(Long.parseLong(id));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public MenuInfo updMenuInfo(MenuInfo MenuInfo) {
        return menuInfoRepository.saveAndFlush(MenuInfo);
    }

    @Override
    public List<MenuInfo> findMenuInfoList() {
        return menuInfoRepository.findAll();
    }

    @Override
    public void deleteMenuInfosByIdIn(List<Long> idList) {
        menuInfoRepository.deleteMenuInfosByIdIn(idList);
    }

    @Override
    public Page<MenuInfo> pageQueryMenuInfoList(int page, int size) {
        return menuInfoRepository.findAll(PageRequest.of(page-1, size));
    }

    @Override
    public Page<MenuInfo> pageQueryMenuInfoByName(String name, int page, int size) {
        Specification querySpecification = (Specification<MenuInfo>) (root,query,cb) ->{
            List<Predicate> predicates = new ArrayList<Predicate>();
            if (null!=name&&!"".equals(name))
            {
                predicates.add(cb.equal(root.get("name").as(String.class),name));
            }
            return cb.and(predicates.toArray(new Predicate[predicates.size()]));
        };
        return menuInfoRepository.findAll(querySpecification,PageRequest.of(page-1, size));
    }
}
